#!/bin/bash
# Job yaml can send below ENVIRONMENT VARIABLES to me
# - registry
# - base_image
# - target_image
# END ENVIRONMENT VARIABLES

. $LKP_SRC/lib/debug.sh

. $LKP_SRC/lib/job.sh
load_job_vars os

check_vars()
{
        [ -n "$registry" ] || die "registry is empty"
        [ -n "$base_image" ] || die "base_image is empty"
        [ -n "$target_image" ] || die "target_image is empty"
}

update_mirror()
{
        if [ $os == "centos" ]
        then
                # uninstall deps
                yum remove -y docker docker-common docker-selinux docker-engine buildah podman

                # install deps
                yum install -y yum-utils device-mapper-persistent-data lvm2 dnf wget

                # add repo
                wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo

                sed -i 's+download.docker.com+opentuna.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
        elif [ $os == "openeuler" ]
        then
                # uninstall deps
                yum remove -y docker-runc docker-compose docker-anaconda-addon docker-engine podman

                # install deps
                yum install -y lvm2 dnf wget

                # add repo
                wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo

                sed -i 's+download.docker.com+opentuna.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
                sed -i 's+$releasever+7+' /etc/yum.repos.d/docker-ce.repo
        elif [ $os == "debian" ]
        then
                # uninstall deps
                apt-get remove -y docker docker-compose docker-clean docker-registry docker.io docker2aci containerd runc buildah podman
                rm -rf /etc/docker && rm -rf /usr/libexec/docker && rm -rf /var/lib/docker && rm -rf /var/lib/containerd && rm -rf /etc/apt/keyrings

                # install deps
                apt-get update && apt-get install -y curl gnupg dpkg lsb-release apt-transport-https ca-certificates software-properties-common lvm2 dnf wget

                # add repo
                mkdir -m 0755 -p /etc/apt/keyrings && curl -fssL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
                echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
                chmod a+r /etc/apt/keyrings/docker.gpg && apt-get update
        fi
}

create_builder()
{
        # install docker
        dnf makecache --timer
        if [ $os == "centos" ]
        then
                yum install -y docker-ce
        elif [ $os == "openeuler" ]
        then
                yum install -y --nobest docker-ce
        elif [ $os == "debian" ]
        then
                apt-get install -y docker-ce
        fi

        # fix on centos7
        mkdir -p /etc/systemd/system/docker.service.d

        cat <<-EOF >/etc/systemd/system/docker.service.d/10-ramdisk.conf
        [Service]
        Environment=DOCKER_RAMDISK=true
        EOF

        # delete docker proxy conf
        local docker_proxy_conf=/etc/systemd/system/docker.service.d/http-proxy.conf
        [ -f "$docker_proxy_conf" ] && rm "$docker_proxy_conf"

        # start docker
        systemctl daemon-reload
        systemctl start docker

        # create mybuilder
        docker buildx create --use --name mybuilder
}

create_toml()
{
        cat <<-EOF > ~/.docker/buildkitd.toml
        debug = true

        # root is where all buildkit state is stored.
        root = "/var/lib/buildkit"

        # insecure-entitlements allows insecure entitlements, disabled by default.
        insecure-entitlements = [ "network.host", "security.insecure"]
        [registry."${registry}"]
          http = true
          insecure = true
        EOF

        docker buildx create --config ~/.docker/buildkitd.toml --use
}

create_dockerfile()
{
        cat <<-EOF > /tmp/Dockerfile
        FROM ${base_image}
        COPY install.sh /root/
        RUN chmod +x /root/install.sh && \
        sed -i 's|repo.openeuler.org|repo.huaweicloud.com/openeuler|g' /etc/yum.repos.d/* && \
        ./root/install.sh
        EOF
}

create_install()
{
        cat <<-EOF > /tmp/install.sh
        #!/bin/bash

        # install deps
        yum install -y openssh git time ruby rpm-build dnf-plugins-core

        # enable sshd login
        sed -i '15a # for openEuler sshd login' /etc/pam.d/sshd
        sed -i '17 s/^/#/' /etc/pam.d/sshd

        # delete install.sh
        rm /root/install.sh
        EOF
}

build_images()
{
        cd /tmp
        systemctl stop docker.socket
        systemctl stop docker
        systemctl restart docker
        docker buildx build --platform linux/amd64,linux/arm64 -t ${registry}/${target_image} --push .
}

main()
{
        check_vars
        update_mirror
        create_builder
        create_toml
        create_dockerfile
        create_install
        build_images
}

main
